package com.hkd.blogsystem.utils;

import org.junit.jupiter.api.Test;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class SecurityUtils {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //密文
        System.out.println(DigestUtils.md5DigestAsHex("123456".getBytes("UTF-8")));
        //颜值
        System.out.println(UUID.randomUUID().toString().replace("-", ""));
    }

    @Test
    void encrypt() throws UnsupportedEncodingException {
         String password = "123456";
         String salt = UUID.randomUUID().toString().replace("-", "");
         String secretString = DigestUtils.md5DigestAsHex((salt+password).getBytes("UTF-8"));
         String sqlPassword = salt + secretString;
        System.out.println(sqlPassword);
    }
    //验证
    @Test
    void verify() throws UnsupportedEncodingException {
        String inputPassword = "123456";
        String sqlPassword = "f65fc5cd0a3649d98d7e82dbff6c5d549c9b291d886645ef13cfbfe76bf3be17";

        String salt = sqlPassword.substring(0, 32);
        String secretString = DigestUtils.md5DigestAsHex((salt+inputPassword).getBytes("UTF-8"));
        boolean result = (salt+secretString).equals(sqlPassword);
        System.out.println(result);
    }
}
